var resources = {
  TWInsightAccount: 'TW洞见公众号',
  TWRecruitAccount: 'TW校招公众号',
  thirdPartyAccount: '第三方公众号',
  weibo: '微博',
  toutiao: '头条',
  zhihu: '知乎',
  douyin: '抖音',
  massMessage: '群发短信',
  massEmail: '群发邮件',
  schoolBBS: '高校 BBS+高校就业网',
  classGroup: '学生：CS院系推文+年级群',
  community: '合作社区',
  poster: '传单+海报+易拉宝',
  PRMedia: 'PR 媒体+办公室媒体',
  official: '官网',
  thirdPartyOfficial: '第三方官网',
  unknown: '直接访问'
}

var collect = {
  data: {
    device: {os: '', inch: '', browser: '', platform: ''},
    extra: {
      activityName: '', userId: '', source: ''
    }
  },
  events: [
    {
      className: 'bbbb',
      eventType: 'click',
      remark: '点击视频'
    }
  ]
}

collect.device = () => {
  const browser = collect._collectBrowser()
  const os = collect._collectOs()
  const inch = collect._collectInch();
  const platform = collect._collectPlatform();
  return {browser, os, inch, platform}
}


collect.extraData = activityName => {
  const userId = collect._collectUserId()
  const source = resources[collect._collectResource()]

  return {userId, source, activityName}
}

collect.onload = activityName => {
  collect.data.device = collect.device()
  collect.data.extra = collect.extraData(activityName)
}


collect.init = (events, activityName) => {
  collect.onload(activityName)
  collect.eventHandler(events)

  sender.sendPv(collect.getData())
}

collect.getData = () => {
  const {extra, device} = collect.data
  return {...extra, ...device, userId: collect._collectUserId()}
}

collect.eventHandler = events => {
  collect.events = events

  events.forEach(event => {
    const {className, eventType, remark} = event

    const elements = document.querySelectorAll(`.${className}`)
    elements.forEach(element => helper.addHandler(element, eventType, () => {
      sender.sendListenEvent({
        ...collect.getData(),
        remark,
        eventType
      })
    }))
  })
}

collect._collectBrowser = () => {
  const {userAgent} = navigator
  if (userAgent.indexOf("Opera") > -1 || userAgent.indexOf("OPR") > -1) return 'Opera'
  if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) return 'IE'
  if (userAgent.indexOf("Edge") > -1) return 'Edge'
  if (userAgent.indexOf("Firefox") > -1) return 'Firefox'
  if (userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") === -1) return 'Safari'
  if (userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1) return 'Chrome'
  if (!!window.ActiveXObject || "ActiveXObject" in window) return 'IE>=11'

  return 'unknown'
}

collect._collectUserId = () => {
  const user = localStorage.getItem('pair_4th_user')
  if (user) {
    return JSON.parse(user).userId
  }
  return null
}

collect._collectOs = () => {
  const {platform} = navigator
  if (platform.indexOf("Win") === 0) return 'windows'
  if (platform.indexOf("Mac") === 0) return 'Mac'
  if (platform === 'X11' || (platform.indexOf("Linux") === 0)) return 'Linux'
  if (navigator.userAgent.match(/iPad/i) !== null) return 'iPad'

  return 'unknown'
}

collect._collectInch = () => {
  return screen.width + "*" + screen.height
}

collect._collectPlatform = () => {
  if ((navigator.userAgent.match(/(pad|pod|iPhone|iPod|ios|iPad)/i))) {
    return 'iphone'
  }
  if ((navigator.userAgent.match(/(phone|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
    return 'android'
  }
  return 'pc'
}

collect._collectResource = () => {
  const params = location.search
  if (params.indexOf('resource') === -1) return 'unknown'

  return params.split('&').find(param => {
      if (param.includes('resource')) {
        return true
      }
      return false
    }
  ).split('=')[1]
}